﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using System.Text;

namespace UpdateEnvironment
{
    class Program
    {
        static void Main(string[] args)
        {
            string FILESPEC32BIT = @"C:\Windows\Microsoft.NET\Framework\v2.0.50727\CONFIG\machine.config";
            string FILESPEC64BIT = @"C:\Windows\Microsoft.NET\Framework64\v2.0.50727\CONFIG\machine.config";

            if (args.Length == 0)
            {
                Console.WriteLine("Usage is " + System.IO.Path.GetFileName(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName) + " Environment");
                Console.WriteLine("Where Environment is Development, Staging, Production etc.");
                Console.WriteLine();
                Console.WriteLine("This program will insert or update the machine.config file");
                Console.WriteLine("for 32bit and, if it exists the 64bit machine.config file.");
                Console.WriteLine("with the environment setting specified.");
                Console.WriteLine("");
                Console.WriteLine("To remove the setting, use the /remove switch.");
                return;
            }
            if (args[0].ToLower() == "/remove")
            {
                RemoveSetting(FILESPEC32BIT);
                RemoveSetting(FILESPEC64BIT);
                return;
            }

            //Check that the Asp.net 2.0 is installed:
            if (!System.IO.File.Exists(FILESPEC32BIT))
            {
                Console.WriteLine("Could not find machine.config file: " + FILESPEC32BIT);
                return;
            }
            UpdateFile(FILESPEC32BIT, args[0]);
            UpdateFile(FILESPEC64BIT, args[0]);
        }

        private static void UpdateFile(string filespec, string NewEnvironment)
        {
            if (!System.IO.File.Exists(filespec))
            {
                return;
            }

            bool bAlreadyExists = false;

            XElement machineconf = XElement.Load(filespec);
            foreach (XNode x in machineconf.Nodes())
            {
                if ((x.ToString().Contains("<appSettings")) && (x.ToString().Contains("Environment")))
                {
                    //Update the value of the node with the new enviroment setting, save the file and exit:
                    XElement Add = new XElement("add",
                            new XAttribute("key", "Environment"),
                            new XAttribute("value", NewEnvironment));
                    XElement NodeToUpdate = new XElement("appSettings", Add);
                    x.ReplaceWith(NodeToUpdate);
                    bAlreadyExists = true;
                }
            }
            if (bAlreadyExists)
            {
                machineconf.Save(filespec);
                Console.WriteLine("Successfully Updated file: " + filespec + " with environment " + NewEnvironment + ".");
                return;
            }

            //Add the key at the bottom of the file:
            XElement Add2 = new XElement("add",
                    new XAttribute("key", "Environment"),
                    new XAttribute("value", NewEnvironment));
            XElement NodeToUpdate2 = new XElement("appSettings", Add2);
            machineconf.Add(NodeToUpdate2);
            machineconf.Save(filespec);
            Console.WriteLine("Successfully Inserted environment " + NewEnvironment + " into file: " + filespec + ".");
            return;

        }

        private static void RemoveSetting(string filespec)
        {
            if (!System.IO.File.Exists(filespec))
            {
                return;
            }

            bool bAlreadyExists = false;

            XElement machineconf = XElement.Load(filespec);
            foreach (XNode x in machineconf.Nodes())
            {
                if ((x.ToString().Contains("<appSettings")) && (x.ToString().Contains("Environment")))
                {
                    //Update the value of the node with the new enviroment setting, save the file and exit:
                    x.Remove();
                    bAlreadyExists = true;
                }
            }
            if (bAlreadyExists)
            {
                machineconf.Save(filespec);
                Console.WriteLine("Successfully Removed setting from: " + filespec);
                return;
            }
            else
            {
                Console.WriteLine("Setting did not exist in: " + filespec);
                return;
            }
        }
    }
}
